		function criaBDSImUsp (){
			var db = window.openDatabase("Database", "1.0", "SImUSP cache", 200000);
			db.transaction(populateDB, errorCB, successCB);
		}
		
		function populateDB(tx) {
			//tx.executeSql('DROP TABLE IF EXISTS SImUSPitem');			
		    tx.executeSql('CREATE TABLE IF NOT EXISTS SImUSPitem (id unique, title, link, publishedDate, downloadDate, category, contentSnippet, content, media)');         
		}		

		function errorCB(tx, err) {
			console.log("Error processing SQL: "+err);
		}
		
		function successCB() {
			console.log("success creating the db!");
		}
		
		function apresentaConteudo() {
	        var db = window.openDatabase("Database", "1.0", "SImUSP cache", 200000);
	        db.transaction(queryDB, errorCB);
		}
		
	    function queryDB(tx) {
	        tx.executeSql('SELECT * FROM SImUSPitem', [], querySuccess, errorCB);
	    }
	    
	    function querySuccess(tx, results) {

	        var len = results.rows.length;
	        for (var i=0; i<len; i++){
	        	
	        	if (i%2){
		        	var conteudoHTML = '<li class="rss_tarja impar '+ results.rows.item(i).category +'" onclick="$("\'.'+results.rows.item(i).id+'\').toggle());" >';
	        	} else {
		        	var conteudoHTML = '<li class="rss_tarja par '+ results.rows.item(i).category +'" onclick="$(".'+results.rows.item(i).id+'").toggle());" >';
	        	}
	        	
	        	conteudoHTML += '<h4>';
	        	conteudoHTML += '<a hrrf="'+results.rows.item(i).link+'" target="_self">';
	        	conteudoHTML += results.rows.item(i).title;
	        	conteudoHTML += '</a>';
	        	conteudoHTML += '</h4>';
	        	conteudoHTML += '<div>';
	        	conteudoHTML += results.rows.item(i).publishedDate;
	        	conteudoHTML += '</div>';
	        	conteudoHTML += '<p>';
	        	conteudoHTML += results.rows.item(i).contentSnippet;
	        	conteudoHTML += '</p>';
	        	conteudoHTML += '<p id="'+results.rows.item(i).id+'" class="conteudo" >';
	        	conteudoHTML += results.rows.item(i).content;
	        	conteudoHTML += '</p>';	        	
	        	conteudoHTML += "</li>";
			
	        	$('#rss_lista').prepend(conteudoHTML);
	        	
				corrigeListaImparPar ();
				
	        }
	    }
	    
		function salvaRss() { 
			var db = window.openDatabase("Database", "1.0", "SImUSP cache", 200000);
			db.transaction(insereEntradasBD, errorCB, successCB);
		}
		
	    function insereEntradasBD(tx) {
	    	$.ajax({
	    		    type: "GET",
	    		    url: "http://www.usp.br/imprensa/?feed=rss2",
	    		    async : false,
	    		    dataType: "xml",
	    		    success: function(xml){
	    		    $(xml).find('item').each(function(){
	    		   
	    		      var title = $(this).find('title').text();
	    		      var link = $(this).find('link').text();
	    		      var pubDate = $(this).find('pubDate').text();
	    		      var category = "";
	    		      $(this).find('category').each(function(){
    		    		  category += "  " + $(this).text();
	    		      });
	    		      var description = $(this).find('description').text();

	    		      var content = $(this).find('content\\:encoded,encoded').text();
	    		      var enclosure  = "e, ";
						var vetorLinkl = link.split("=");
						var id = vetorLinkl[vetorLinkl.length-1];
						var downloadDate = new Date();
	    		        var sql = 'INSERT INTO SImUSPitem (id , title, link, publishedDate, downloadDate, category, contentSnippet, content, media) VALUES (?,?,?,?,?,?,?,?,?)';
				        //alert ((sql,  [id, title, link, pubDate, downloadDate, category, description, content, enclosure ])+id+ title+ link+ pubDate+ downloadDate+ category+ description+ content+ enclosure);
				    	tx.executeSql(sql, [id, title, link, pubDate, downloadDate, category, description, content, enclosure ]);        
	    		    });
	    		  },
	    		  error: function() {
	    			  console.log("!!!Erro ao processar o xml!!!");
	    		  }
	  		});
	    	recarregaNoticiasNaTela();
	    	
		    tx.executeSql('SELECT * FROM SImUSPitem', [], limpaCache, errorCB);

	    }
	    
	    function recarregaNoticiasNaTela() {
	    	$('.rss_tarja').remove();
	    	apresentaConteudo();
	    	$('#alertas').html(" ");
	    	var d = new Date();
			window.localStorage.setItem("dataUltimaAtualizacao", d.getDate()+"/"+(	d.getMonth()+1)+"/"+d.getFullYear());
	    	apresentaDataAtualizacao();	    	
	    }
	    
	    function limpaCache(tx, results) {

	        var len = results.rows.length;
	        var tamanhoCache = window.localStorage.getItem("tamanhoCache");
	        if (len>tamanhoCache){
	        	for (var i=0; i<len-tamanhoCache; i++){
	        		var sql = 'DELETE FROM SImUSPitem WHERE id='+results.rows.item(len-i-1).id;
	        		tx.executeSql(sql);  
	        	}
	        }
	    }